import 'package:flutter/material.dart';
import 'package:wayofdressing/contraints.dart';
import 'package:wayofdressing/screens/own_screen.dart';

class PostScreen extends StatefulWidget {
  static final String id = 'postScreen';

  @override
  State<PostScreen> createState() => _PostScreenState();
}

class _PostScreenState extends State<PostScreen> {
  String title = '';
  String content = '';
  String authorName = '';
  int commentsCount = 0;
  int sharesCount = 0;
  int favoritesCount = 0;
  int authorId = 0;
  int likesCount = 0;
  late ImageProvider headImage;
  late ImageProvider image;
  bool isLoad = false;

  Future<void> getPostInfo() async {
    final Map<String, dynamic>? args =
        ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?;
    setState(() {
      title = args?['postInfo']['title'] ?? '';
      content = args?['postInfo']['content'] ?? '';
      authorName = args?['postInfo']['authorName'] ?? '';
      likesCount = args?['postInfo']['likesCount'] ?? 0;
      commentsCount = args?['postInfo']['commentsCount'] ?? 0;
      sharesCount = args?['postInfo']['sharesCount'] ?? 0;
      favoritesCount = args?['postInfo']['favoritesCount'] ?? 0;
      authorId = args?['postInfo']['authorId'] ?? 0;
      headImage = args?['headImage'] ?? AssetImage('images/peanut/jpg');
      image = args?['image'] ?? AssetImage('images/peanut/jpg');
      isLoad = true;
    });
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    if (!isLoad) {
      getPostInfo();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xfffffaf0),
      appBar: AppBar(
        backgroundColor: Color(0xfffffaf0),
        elevation: 0,
        toolbarHeight: 80,
        leadingWidth: 200,
        leading: Row(
          children: [
            SizedBox(
              width: 8,
            ),
            GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: Transform.scale(
                scaleX: -1,
                child: Icon(
                  Icons.play_arrow_rounded,
                  size: kAppBarButtonSize,
                  color: Colors.black.withOpacity(0.5),
                ),
              ),
            ),
            Row(
              children: [
                isLoad
                    ? GestureDetector(
                        onTap: () {
                          Navigator.pushNamed(context, OwnScreen.id,arguments: {'name':authorName,'id':authorId,'headImage':headImage});
                        },
                        child: CircleAvatar(
                          backgroundImage: headImage,
                          radius: 20,
                        ),
                      )
                    : SizedBox()
              ],
            ),
            SizedBox(
              width: 10,
            ),
            Text(
              authorName,
              style: TextStyle(fontSize: 15, color: Colors.black),
            )
          ],
        ),
        actions: [
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 22.0),
            child: TextButton(
              style: ButtonStyle(
                side: MaterialStateProperty.all(
                  BorderSide(color: Colors.redAccent, width: 1),
                ),
                shape: MaterialStateProperty.all(
                  RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(18),
                  ),
                ),
              ),
              onPressed: () {},
              child: Text(
                '关注',
                style: TextStyle(color: Colors.redAccent),
              ),
            ),
          ),
          SizedBox(
            width: 10,
          ),
          GestureDetector(
            onTap: () {},
            child: Icon(
              Icons.share_rounded,
              size: 40,
              color: Colors.black,
            ),
          ),
          SizedBox(
            width: 20,
          )
        ],
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          isLoad
              ? Flexible(
                  child: Image(
                    image: image,
                  ),
                )
              : SizedBox(),
          Container(
            margin: EdgeInsets.symmetric(horizontal: 15),
            alignment: Alignment.topLeft,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                  softWrap: true,
                ),
                SizedBox(
                  height: 5,
                ),
                Text(
                  content,
                  softWrap: true,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
